var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser')
var logger = require('morgan');
const cors = require('cors')
//路由
const loginRouter = require('./routes/login/login.js') 
const menuRouter = require('./routes/menu/menu.js')
const usersRouter = require('./routes/users/users.js')
const articleRouter = require('./routes/article/article.js')
const imagesRouter = require('./routes/images/upImages')
const noticeRouter = require('./routes/notice/notice.js')
const bannerRouter = require('./routes/banner/banner.js')
const artRouter = require('./routes/art/art.js')
const dynamicRouter = require('./routes/dynamic/dynamic.js')
const joinRouter = require('./routes/join/join.js')
const contactRouter = require('./routes/contact/contact.js')
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

//中间件
//跨域中间件
app.use(cors())

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(bodyParser.json());

app.use('/public', express.static(path.join(__dirname, 'public')));
app.use('/img',express.static(path.join(__dirname,'routes/banner/img')))
app.use('/imgArt',express.static(path.join(__dirname,'routes/art/img')))
app.use('/imgDynamic',express.static(path.join(__dirname,'routes/dynamic/img')))
app.use('/upload',express.static(path.join(__dirname,'routes/images/uploads')))

//路由中间件
app.use('/login',loginRouter)
app.use('/menu',menuRouter)
app.use('/users',usersRouter)
app.use('/article',articleRouter)
app.use('/images',imagesRouter)
app.use('/notice',noticeRouter)
app.use('/banner',bannerRouter)
app.use('/art',artRouter)
app.use('/dynamic',dynamicRouter)
app.use('/join',joinRouter)
app.use('/contact',contactRouter)
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

app.listen(3000,() => {
  console.log('api3000端口打开')
})


module.exports = app;
